<?php

class UsersModel extends CI_Model{

	private $table = 'users';

	function __construct(){
		parent::__construct();
	}

	public function addUser($data){
		// $this->db->insert($this->table,$data);
		// return $this->db->insert_id();
	}

	//检查邮箱是否存在
	public function check_email_address($email){
		$sql = "select count(*) as count from users where email=?";
		$result = $this->db->query($sql,array($email));
		$row = $result->row_array();
		if ($row['count'] == 0){
			return true;
		}else{
			return false;
		}
	}

	//检查用户名是否存在
	public function check_userName($userName){
		$sql = "select count(*) as count from users where username=?";
		$result = $this->db->query($sql,array($userName));
		$row = $result->row_array();
		if ($row['count'] == 0){
			return true;
		}else{
			return false;
		}
	}

	//检查登陆
	public function check_login($userName,$password){
		$sql = "select * from users where userName=? AND password=?";
		$query = $this->db->query($sql,array($userName,md5($password)));
		$row = $query->row_array();
		return $row;
	}

	//更新登录时间
	public function update_login_date($userId){

		$sql = "update users set last_login_date=? where id=?";
		$this->db->query($sql,array(date('Y-m-d H:i:s'),$userId));
	}

	//根据用户ID获得用户名
	public function get_name_by_id($userId){

		$sql = "select username from users where id = ?";
		$result = $this->db->query($sql,array($userId));
		$row = $result->row_array();
		if(!$row){
			return null;
		}else{
			return $row['username'];
		}
		
	}

	//添加注册用户
	public function add_user($data){
		$this->db->insert($this->table,$data);
	}
}